package cn.cdu.medical.mapper;

import cn.cdu.medical.entity.ChatMessage;
import cn.cdu.medical.dto.ConsultationIdWithTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ChatMessageMapper extends BaseMapper<ChatMessage> {
    
    @Select("SELECT * FROM chat_message WHERE consultation_id = #{consultationId} ORDER BY create_time ASC")
    List<ChatMessage> getMessagesByConsultationId(@Param("consultationId") String consultationId);
    
    @Select("SELECT * FROM chat_message WHERE user_id = #{userId} ORDER BY create_time ASC")
    List<ChatMessage> getMessagesByUserId(@Param("userId") String userId);
    
    @Select("SELECT consultation_id as consultationId, MAX(create_time) as createTime " +
           "FROM chat_message WHERE user_id = #{userId} " +
           "GROUP BY consultation_id " +
           "ORDER BY MAX(create_time) DESC " +
           "LIMIT #{limit}")
    List<ConsultationIdWithTime> getRecentConsultationIds(@Param("userId") String userId, @Param("limit") int limit);
} 
 
 
 